<template>
  <div>
    <el-card>
      <template #header>
        <h4>店铺管理</h4>
      </template>
      <div>
        <el-form
          ref="ruleForm"
          :rules="rules"
          :model="shopForm"
          label-width="80px"
        >
          <el-form-item label="店铺名称" prop="name">
            <el-input v-model="shopForm.name"></el-input>
          </el-form-item>

          <el-form-item label="店铺公告" prop="bulletin">
            <el-input type="textarea" v-model="shopForm.bulletin"></el-input>
          </el-form-item>

          <el-form-item label="店铺头像">
            <el-upload
              class="avatar-uploader"
              action="http://172.16.100.203:5000/shop/upload"
              :show-file-list="false"
              :on-success="uploadSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="店铺图片" style="width: 1000px;">
            <el-upload
              action="http://172.16.100.203:5000/shop/upload"
              list-type="picture-card"
              :on-success="handleAvatarSuccess"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :file-list="fileList"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="" />
            </el-dialog>
          </el-form-item>

          <el-form-item label="配送费" prop="deliveryPrice">
            <el-input v-model="shopForm.deliveryPrice"></el-input>
          </el-form-item>
          <el-form-item label="配送时间" prop="deliveryTime">
            <el-input v-model="shopForm.deliveryTime"></el-input>
          </el-form-item>
          <el-form-item label="配送描述" prop="description">
            <el-input v-model="shopForm.description"></el-input>
          </el-form-item>
          <el-form-item label="店铺评分" prop="score">
            <el-input v-model="shopForm.score"></el-input>
          </el-form-item>
          <el-form-item label="销量" prop="sellCount">
            <el-input v-model="shopForm.sellCount"></el-input>
          </el-form-item>
          <el-form-item label="活动" prop="supports">
            <el-checkbox-group v-model="shopForm.supports">
              <el-checkbox
                label="在线支付满28减5"
                name="supports"
              ></el-checkbox>
              <el-checkbox
                label="VC无限橙果汁全场8折"
                name="supports"
              ></el-checkbox
              ><br />
              <el-checkbox label="单人精彩套餐" name="supports"></el-checkbox>
              <el-checkbox label="特价饮品8折抢购" name="supports"></el-checkbox
              ><br />
              <el-checkbox label="担任特色套餐" name="supports"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>

          <el-form-item label="营业时间">
            <el-time-picker
              is-range
              arrow-control
              v-model="shopForm.date"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择时间范围"
            >
            </el-time-picker>
          </el-form-item>

          <el-button type="primary" @click="submitForm('ruleForm')"
            >保存修改</el-button
          >
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { shopInfo, shopEdit } from '../../api/info.js'
import _ from 'lodash'
export default {
  data() {
    return {
      shopForm: {
        supports: [],
        date: [],
        avatar: '',
        pics: [],
        id: '',
        name: '',
        bulletin: '',
        deliveryPrice: '',
        deliveryTime: '',
        description: '',
        score: '',
        sellCount: ''
      },
      url: 'http://127.0.0.1:5000/upload/shop/',
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [],

      rules: {
        name: [{ required: true, message: '请填写店铺名称', trigger: 'blur' }],
        bulletin: [{ required: true, message: '请填写公告', trigger: 'blur' }],
        deliveryPrice: [
          { required: true, message: '请填写所需配送费', trigger: 'blur' }
        ],
        deliveryTime: [
          { required: true, message: '请填写配送时间', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请填写配送描述', trigger: 'blur' }
        ],
        score: [{ required: true, message: '请输入店铺评分', trigger: 'blur' }],
        sellCount: [
          { required: true, message: '请输入店铺销量', trigger: 'blur' }
        ],
        supports: [
          {
            type: 'array',
            required: true,
            message: '请至少选择一个活动性质',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created() {
    this.getInfo()
  },
  methods: {
    // 获取店铺详情
    async getInfo() {
      const { data } = await shopInfo()
      console.log(data)
      const arr = []
      this.imageUrl = this.url + data.avatar
      this.fileList = []
      data.pics.forEach(item => {
        arr.push({
          name: item,
          url: this.url + item
        })
      })
      this.fileList = arr
      this.shopForm = data
    },

    // 修改店铺内容
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const editObj = _.cloneDeep(this.shopForm)
          editObj.pics = JSON.stringify(editObj.pics)
          editObj.supports = JSON.stringify(editObj.supports)
          editObj.date = JSON.stringify(editObj.date)
          const data = await shopEdit(editObj)
          console.log(data)
          if (data.code === 0) {
            location.reload()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // 图片上传
    uploadSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.shopForm.avatar = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 照片墙
    handleRemove(file, fileList) {
      console.log(file, fileList)
      // 拿到删除的那个图片的索引,然后根据索引删除数组中的
      const i = this.shopForm.pics.findIndex(item => item === file.name)
      this.shopForm.pics.splice(i, 1)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleAvatarSuccess(res, file) {
      this.shopForm.pics.push(res.imgUrl)
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-textarea__inner {
  width: 400px;
  height: 150px;
}
/deep/.el-input__inner {
  width: 400px;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
